✨ BCA JUL24 Batch ✨

Join Our WhatsApp Group

Anukasif Pic

5.3 - Unions - MCQs

Interactive MCQs Quiz

Test your knowledge with these questions

1. What is a union in C?

2. How much memory does a union occupy?

3. Which keyword is used to declare a union in C?

4. How are union members accessed in C?

5. Which of the following is a correct way to declare a union?

6. What happens when you assign a value to a member of a union?

7. How does the memory allocation for a union differ from that of a structure?

8. Which is true regarding unions in C?

9. What is the size of the union below if char is 1 byte, int is 4 bytes, and float is 4 bytes?

                        union example {
                            char ch;
                            int a;
                            float b;
                        };
                    

10. What is the primary advantage of using a union over a structure?

11. How can you declare a variable for a union named data in C?

12. What will be the output of the following code if char is 1 byte and int is 4 bytes?

                    union example {
                        char ch;
                        int a;
                    };
                    union example ex;
                    printf("%lu", sizeof(ex));
                    

13. Which of the following is a valid difference between unions and structures?

14. Can a union store multiple values simultaneously?

15. Which statement is true about the following union?

                    union myUnion {
                        int a;
                        float b;
                    };
                    

16. Which is the correct syntax to access a union member?

17. What is the size of the following union?

                    union myUnion {
                        double d;
                        char ch[5];
                    };
                    

18. In a union, which member is initialized last in memory?

19. What is the correct output of the following code?

                    union example {
                        int a;
                        float b;
                    };
                    union example ex;
                    ex.a = 10;
                    ex.b = 3.14;
                    printf("%d", ex.a);
                    

20. Which of the following operations is NOT valid for unions in C?

21. What is a nested union in C?

22. Which keyword is used to declare a nested union?

23. In a nested union, which memory size is allocated for the union?

24. What is the benefit of using nested unions in C?

25. Which of the following is a valid way to access a member of a nested union?

26. In the following code, what is the type of myUnion.innerUnion.innerChar?

                    union OuterUnion {
                        int outerInt;
                        union InnerUnion {
                            char innerChar;
                            double innerDouble;
                        } innerUnion;
                    };
                    

27. What is a key difference between structures and unions in C?

28. How are members of a union accessed?

29. What happens if you assign a value to the outerInt and then access innerChar in the following union?

                    union OuterUnion {
                        int outerInt;
                        union InnerUnion {
                            char innerChar;
                            double innerDouble;
                        } innerUnion;
                    };
                    

30. Which of the following demonstrates the correct way to initialize a nested union?

31. In the given code, which member of the outer union takes up the most memory?

                        union OuterUnion {
                            int outerInt;
                            union InnerUnion {
                                char innerChar;
                                double innerDouble;
                            } innerUnion;
                        };
                    

32. Which of the following is an advantage of using unions in C?

33. How many members of a nested union can be active at the same time?

34. In the following example, what will the size of OuterUnion be?

                        union OuterUnion {
                            int outerInt;
                            float outerFloat;
                            union InnerUnion {
                                char innerChar;
                                double innerDouble;
                            } innerUnion;
                        };
                    

35. Which member of the nested union will be accessed in this code?

                        union OuterUnion myUnion;
                        myUnion.innerUnion.innerChar = 'B';
                        printf("%c", myUnion.innerUnion.innerChar);
                    

36. What happens to the value of the outer union when you assign a value to a member of the inner union?

37. In the following code, which member is part of the inner union?

                        union OuterUnion {
                            int outerInt;
                            union InnerUnion {
                                char innerChar;
                                double innerDouble;
                            } innerUnion;
                        };
                    

38. What does the following code print?

                        union OuterUnion {
                            int outerInt;
                            union InnerUnion {
                                char innerChar;
                                double innerDouble;
                            } innerUnion;
                        };
                        union OuterUnion myUnion;
                        myUnion.innerUnion.innerDouble = 3.14;
                        printf("%f", myUnion.innerUnion.innerDouble);
                    

39. How is memory allocated when a union is nested within another union?

40. Can a union be nested inside a structure in C?

41. What is an array of unions in C?

42. How is an array of unions declared in C?

43. In an array of unions, each element in the array:

44. What is the memory size allocated for each element in an array of unions?

45. Which of the following operations is valid when using an array of unions?

46. In the following code, what type is stored in arrayOfUnions[1]?

                        union MyUnion {
                            int intValue;
                            float floatValue;
                            char charValue;
                        };
                        union MyUnion arrayOfUnions[5];
                        arrayOfUnions[1].floatValue = 3.14;
                    

47. What happens if you assign values to two members of a union at the same index in an array of unions?

48. What is the benefit of using arrays of unions?

49. How do you access an element of a union inside an array of unions?

50. In the following example, what value will be printed for arrayOfUnions[2].charValue?

                        union MyUnion {
                            int intValue;
                            float floatValue;
                            char charValue;
                        };
                        union MyUnion arrayOfUnions[5];
                        arrayOfUnions[2].charValue = 'A';
                        printf("%c", arrayOfUnions[2].charValue);
                    

51. Can an array of unions contain elements with different data types?

52. Which of the following statements is true about arrays of unions?

53. In the following code, what is the output for items[1].price?

                    union ItemAttributes {
                        int weight;
                        float price;
                        char category;
                    };
                    union ItemAttributes items[3];
                    items[1].price = 19.99;
                    printf("%.2f", items[1].price);
                    

54. How is memory allocated for arrays of unions?

55. Can arrays of unions be passed as function arguments?

56. What would happen if you try to print an uninitialized element of an array of unions?

57. Which of the following correctly initializes an element in an array of unions?

58. In an array of unions, each element is initialized:

59. Can arrays of unions be dynamically allocated?

60. What is a key difference between arrays of unions and arrays of structures?

61. How can you pass a union to a function in C?

62. What happens when you pass a union by value to a function?

63. What is the syntax to pass a specific member of a union to a function?

64. Why would you pass a union by reference to a function?

65. Which operator is used to access members of a union when passed as a pointer to a function?

66. What is the result of passing a union pointer to a function?

67. How can you ensure type safety when passing union members to a function?

68. What happens if you pass the wrong union member type to a function?

69. How is memory handled when passing a union by value to a function?

70. Which of the following is a valid function signature for passing a union by reference?

71. When passing a union pointer to a function, how can you modify a member of the union?

72. What is the benefit of passing specific union members rather than the entire union?

73. Which of the following is true about passing unions by value to a function?

74. How do you pass a union as an argument to a function if you want to avoid copying the union?

75. Which keyword is used to define a union in C?

76. What is the primary difference between passing a structure and a union to a function?

77. How do you pass multiple members of a union to a function at the same time?

78. What is a possible drawback of passing union members to a function?

79. How can you prevent modification of a union passed to a function?

80. What happens when a union is passed by reference to a function?